package eu.tolu.cableSpotter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import eu.tolu.cableSpotter.help.AppPreferences;
import eu.tolu.cableSpotter.help.BuildCableArray;
import eu.tolu.cableSpotter.R;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SpotList extends ListActivity {

	private ArrayList<String> cableAdapterArray = new ArrayList<String>();
	private ArrayList<ArrayList<String>> cableArray = new ArrayList<ArrayList<String>>();
	private ArrayAdapter<String> adapter;
	public String country = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spot_list);
		
		final Bundle extras = getIntent().getExtras();

		if (extras != null) {
			country = extras.getString("country");
			final TextView txtSelectedCountry = (TextView) findViewById(R.id.tx_selected_country);
			txtSelectedCountry.setText(country);
		}
		
		new RetreiveFeedTask(this).execute();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, cableAdapterArray);
		setListAdapter(adapter);
		
	}
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		Intent intent = new Intent(this, SpotView.class);

		for (ArrayList<String> cabData : cableArray) {
			if (cabData.get(1).equals(l.getItemAtPosition(position).toString())) {
				try {
					intent.putExtra("country", cabData.get(0));
					intent.putExtra("location", cabData.get(1));
					intent.putExtra("add1", cabData.get(2));
					intent.putExtra("add2", cabData.get(3));
					intent.putExtra("add3", cabData.get(4));
					intent.putExtra("addInfo", cabData.get(5));
					intent.putExtra("phone", cabData.get(6));
					intent.putExtra("fax", cabData.get(7));
					intent.putExtra("email", cabData.get(8));
					intent.putExtra("web", cabData.get(9));
					intent.putExtra("img", cabData.get(10));
					intent.putExtra("custome", cabData.get(11));
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}

		startActivity(intent);
	}

	public void setCableArray(ArrayList<ArrayList<String>> cablesArray)
			throws IOException {
		System.out.println("SET CABLE ARRAY :"+cablesArray.size());
		cableAdapterArray.clear();
		cableArray.clear();
		cableArray = cablesArray;
		for(ArrayList<String> cab : cablesArray){
			cableAdapterArray.add(cab.get(1));
			adapter.notifyDataSetChanged();
		}
	}
	

}

// -----------------------------------------------

class RetreiveFeedTask extends AsyncTask<String, Void, String> {

	private SpotList mainContext = null;
	private ArrayList<ArrayList<String>> cablesArray = new ArrayList<ArrayList<String>>();

	public RetreiveFeedTask(SpotList context) {
		mainContext = context;
		Log.v("LongOper", "Konstruktor");
	}

	@Override
	protected String doInBackground(String... params) {
		AppPreferences appPreferences = new AppPreferences(mainContext);
		cablesArray = appPreferences.getCableArrayByCountry(mainContext.country);
		return "test";
	}

	@Override
	protected void onPostExecute(String result) {
		Log.v("LongOper", "Set Array");
		try {
			mainContext.setCableArray(cablesArray);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
